package org.jgs1904.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jgs1904.bean.ResultBean;
import org.jgs1904.entity.Region;
import org.jgs1904.service.RegionService;
import org.jgs1904.service.impl.RegionServiceImpl;

import com.alibaba.fastjson.JSON;

/**
 * 区划控制器
 * 
 * @author junki
 * @date 2020年3月26日
 */
@WebServlet("/region")
public class RegionController extends DispatcherController {

	private static final long serialVersionUID = -9131206594803720497L;
	
	private RegionService regionService = new RegionServiceImpl();

	/**
	 * 获取省级区划
	 * @throws IOException 
	 */
	public void province(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		List<Region> provinceList = null;
		
		try {
			provinceList = regionService.getProvince();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		ResultBean resultBean = ResultBean.builder().code("200").message("ok").data(provinceList).build();
		
		response.getWriter().print(JSON.toJSONString(resultBean));
		
	} 
	
	/**
	 * 根据父级id获取子级区划
	 * @throws IOException 
	 */
	public void child(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String parentId = request.getParameter("parent_id");
		
		List<Region> regionList = null;
		
		try {
			regionList = regionService.getChild(parentId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		ResultBean resultBean = ResultBean.builder().code("200").message("ok").data(regionList).build();
		
		response.getWriter().print(JSON.toJSONString(resultBean));
		
	} 
}
